/*
 * @Author: DESKTOP-DOGAKD5\86185 134668
 * @Date: 2022-11-10 11:33:05
 * @LastEditors: DESKTOP-DOGAKD5\86185 134668
 * @LastEditTime: 2022-11-10 12:08:28
 * @FilePath: \vue2-demo\src\store\index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import Vue from 'vue'
import Vuex from 'vuex'
import request from '@/utils/request'

Vue.use(Vuex)

// 用来存储数据
const state = {
        collectionList: [],
        value: '',
        login: window.localStorage.getItem('login'),
        detail: window.localStorage.getItem('detail'),
    }
    // 响应组件中的事件
const actions = {
        fetchCollectionList: async({ commit }) => {
            const res = await request.get("/my/collection/list");
            commit('setCollectionList', res.data)
        },
        deleteCollectionList(state, value) {
            state.collectionList = value
        },
    }
    // 操作数据
const mutations = {
        setCollectionList(state, value) {
            state.collectionList = value
        },
        setValue(state, value) {
            state.value = value
        },
        setLogin(state, value) {
            console.log('====================================');
            console.log(value);
            console.log('====================================');
            if (value) {
                window.localStorage.setItem('login', JSON.stringify(value))
                state.login = window.localStorage.getItem('login')
            } else {
                window.localStorage.removeItem('login')
                state.login = null
            }
        },
        setdetail(state, value) {
            if (value) {
                window.localStorage.setItem('detail', JSON.stringify(value))
                state.detail = window.localStorage.getItem('detail')
            } else {
                window.localStorage.removeItem('detail')
                state.detail = null
            }
        },
    }
    // 用来将state数据进行加工
const getters = {
        getCollectionData() {
            return state.collectionList
        },
        getValue(state) {
            return state.value
        },
        getdetail(state) {
            return JSON.parse(state.detail)
        },
        getLogin(state) {
            console.log('====================================');
            console.log(state.login);
            console.log('====================================');
            return JSON.parse(state.login)
        },
    }
    // 新建并暴露store
export default new Vuex.Store({
    state,
    actions,
    mutations,
    getters
})